<template>
  <el-drawer
    v-model="visible"
    title="系统设置"
    direction="rtl"
    size="320px"
    :close-on-click-modal="false"
    :z-index="3000"
    :append-to-body="true"
    :modal="true"
  >
    <div class="settings-container">
      <!-- 布局模式 -->
      <div class="setting-section">
        <h3 class="section-title">
          <el-icon><Grid /></el-icon>
          布局模式
        </h3>
        <div class="layout-options">
          <div 
            class="layout-item" 
            :class="{ active: appStore.settings.Layout === 'vertical' }"
            @click="updateLayout('vertical')"
          >
            <div class="layout-preview layout-vertical">
              <div class="layout-sidebar">
                <div class="sidebar-logo"></div>
                <div class="sidebar-menu">
                  <div class="menu-item"></div>
                  <div class="menu-item"></div>
                  <div class="menu-item"></div>
                </div>
              </div>
              <div class="layout-main">
                <div class="layout-header">
                  <div class="header-breadcrumb"></div>
                </div>
                <div class="layout-tabs"></div>
                <div class="layout-content"></div>
              </div>
            </div>
            <span>左侧菜单</span>
          </div>
          
          <div 
            class="layout-item" 
            :class="{ active: appStore.settings.Layout === 'horizontal' }"
            @click="updateLayout('horizontal')"
          >
            <div class="layout-preview layout-horizontal">
              <div class="layout-header">
                <div class="header-logo"></div>
                <div class="header-menu">
                  <div class="menu-item"></div>
                  <div class="menu-item"></div>
                  <div class="menu-item"></div>
                </div>
              </div>
              <div class="layout-tabs"></div>
              <div class="layout-content"></div>
            </div>
            <span>顶部菜单</span>
          </div>
          
          <div 
            class="layout-item" 
            :class="{ active: appStore.settings.Layout === 'mix' }"
            @click="updateLayout('mix')"
          >
            <div class="layout-preview layout-mix">
              <div class="layout-left">
                <div class="left-logo"></div>
                <div class="left-menu">
                  <div class="menu-item"></div>
                  <div class="menu-item active"></div>
                  <div class="menu-item"></div>
                </div>
              </div>
              <div class="layout-right">
                <div class="layout-header">
                  <div class="header-breadcrumb"></div>
                </div>
                <div class="layout-tabs"></div>
                <div class="layout-body">
                  <div class="secondary-menu">
                    <div class="menu-item"></div>
                    <div class="menu-item"></div>
                  </div>
                  <div class="layout-content"></div>
                </div>
              </div>
            </div>
            <span>混合模式</span>
          </div>
        </div>
      </div>

      <!-- 主题色 -->
      <div class="setting-section">
        <h3 class="section-title">
          <el-icon><Brush /></el-icon>
          主题色
        </h3>
        <div class="theme-colors">
          <div 
            v-for="color in themeColors" 
            :key="color.value"
            class="color-item"
            :class="{ active: appStore.settings.EpThemeColor === color.value }"
            @click="updateThemeColor(color.value)"
          >
            <div class="color-preview" :style="{ background: color.value }">
              <el-icon v-if="appStore.settings.EpThemeColor === color.value" class="check-icon">
                <Check />
              </el-icon>
            </div>
            <span class="color-name">{{ color.name }}</span>
          </div>
        </div>
      </div>

      <!-- 界面设置 -->
      <div class="setting-section">
        <h3 class="section-title">
          <el-icon><Setting /></el-icon>
          界面设置
        </h3>
        <div class="setting-items">
          <div class="setting-item">
            <span>显示面包屑</span>
            <el-switch 
              v-model="appStore.settings.ShowBreadcrumb"
              @change="updateSetting('ShowBreadcrumb', $event)"
            />
          </div>
          
          <div class="setting-item">
            <span>显示标签页</span>
            <el-switch 
              :model-value="!appStore.settings.HideTabs"
              @change="updateSetting('HideTabs', !$event)"
            />
          </div>
          
          <div class="setting-item">
            <span>标签页缓存</span>
            <el-switch 
              v-model="appStore.settings.MultiTagsCache"
              @change="updateSetting('MultiTagsCache', $event)"
            />
          </div>
          
          <div class="setting-item">
            <span>页面 KeepAlive</span>
            <el-switch 
              v-model="appStore.settings.KeepAlive"
              @change="updateSetting('KeepAlive', $event)"
            />
          </div>
        </div>
      </div>

      <!-- 主题模式 -->
      <div class="setting-section">
        <h3 class="section-title">
          <el-icon><Sunny /></el-icon>
          主题模式
        </h3>
        <div class="setting-items">
          <div class="setting-item">
            <span>灰色模式</span>
            <el-switch 
              v-model="appStore.settings.Grey"
              @change="updateSetting('Grey', $event)"
            />
          </div>
          
          <div class="setting-item">
            <span>色弱模式</span>
            <el-switch 
              v-model="appStore.settings.Weak"
              @change="updateSetting('Weak', $event)"
            />
          </div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="setting-actions">
        <el-button type="primary" @click="handleSave" :icon="Check">
          保存设置
        </el-button>
        <el-button @click="handleReset" :icon="RefreshRight">
          恢复默认
        </el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { ref } from 'vue';
import { useAppStore } from '@/store/app';
import { ElMessage } from 'element-plus';
import { 
  Grid, 
  Brush, 
  Setting, 
  Sunny, 
  Check,
  RefreshRight
} from '@element-plus/icons-vue';

const appStore = useAppStore();
const visible = ref(false);

// 预设主题色
const themeColors = [
  { name: '经典蓝', value: '#409EFF' },
  { name: '优雅紫', value: '#722ED1' },
  { name: '活力绿', value: '#52C41A' },
  { name: '阳光橙', value: '#FA8C16' },
  { name: '热情红', value: '#F5222D' },
  { name: '浪漫粉', value: '#EB2F96' },
  { name: '清新青', value: '#13C2C2' },
  { name: '天空蓝', value: '#1890FF' },
];

// 打开抽屉
const open = () => {
  visible.value = true;
};

// 关闭抽屉
const close = () => {
  visible.value = false;
};

// 更新布局
const updateLayout = (layout) => {
  appStore.updateSetting('Layout', layout);
};

// 更新主题色
const updateThemeColor = (color) => {
  appStore.updateSetting('EpThemeColor', color);
  // 动态修改 Element Plus 主题色和相关颜色变量
  document.documentElement.style.setProperty('--el-color-primary', color);
  document.documentElement.style.setProperty('--el-color-primary-light-3', adjustColor(color, 0.7));
  document.documentElement.style.setProperty('--el-color-primary-light-5', adjustColor(color, 0.5));
  document.documentElement.style.setProperty('--el-color-primary-light-7', adjustColor(color, 0.3));
  document.documentElement.style.setProperty('--el-color-primary-light-8', adjustColor(color, 0.15));
  document.documentElement.style.setProperty('--el-color-primary-light-9', adjustColor(color, 0.08));
  document.documentElement.style.setProperty('--el-color-primary-dark-2', adjustColor(color, -0.2));
};

// 优化的颜色混合函数 - 使用更好的算法
const adjustColor = (color, amount) => {
  const num = parseInt(color.replace('#', ''), 16);
  const r = (num >> 16) & 0xFF;
  const g = (num >> 8) & 0xFF;
  const b = num & 0xFF;
  
  // 如果是正值（变浅），与白色混合；负值（变深），与黑色混合
  const mix = amount > 0 ? 255 : 0;
  const abs = Math.abs(amount);
  
  const newR = Math.round(r + (mix - r) * abs);
  const newG = Math.round(g + (mix - g) * abs);
  const newB = Math.round(b + (mix - b) * abs);
  
  return '#' + ((1 << 24) + (newR << 16) + (newG << 8) + newB).toString(16).slice(1);
};

// 更新设置
const updateSetting = (key, value) => {
  appStore.updateSetting(key, value);
};

// 保存设置
const handleSave = () => {
  ElMessage.success('设置已保存');
};

// 恢复默认
const handleReset = () => {
  appStore.resetSettings();
  ElMessage.info('已恢复默认设置');
};

// 暴露方法给父组件
defineExpose({
  open,
  close,
});
</script>

<style scoped>
.settings-container {
  padding: 0 0 20px 0;
}

.setting-section {
  margin-bottom: 32px;
  /* padding: 0 20px; */
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 16px 0;
}

.section-title .el-icon {
  color: #409EFF;
}

/* 布局选项 */
.layout-options {
  display: flex;
  gap: 12px;
}

.layout-item {
  flex: 1;
  cursor: pointer;
  transition: all 0.3s;
}

.layout-item:hover .layout-preview {
  border-color: #409EFF;
  transform: translateY(-2px);
}

.layout-item.active .layout-preview {
  border-color: #409EFF;
  box-shadow: 0 2px 12px rgba(64, 158, 255, 0.3);
}

.layout-item span {
  display: block;
  text-align: center;
  font-size: 12px;
  color: #606266;
  margin-top: 8px;
}

.layout-item.active span {
  color: #409EFF;
  font-weight: 600;
}

.layout-preview {
  height: 70px;
  border: 2px solid #DCDFE6;
  border-radius: 6px;
  overflow: hidden;
  transition: all 0.3s;
  background: #F5F7FA;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* === 左侧菜单布局 === */
.layout-vertical {
  display: flex;
  height: 100%;
}

.layout-vertical .layout-sidebar {
  width: 22%;
  background: linear-gradient(180deg, #545c64 0%, #304156 100%);
  display: flex;
  flex-direction: column;
  padding: 3px;
}

.layout-vertical .sidebar-logo {
  height: 8px;
  background: rgba(255, 255, 255, 0.15);
  margin: 2px;
  border-radius: 2px;
}

.layout-vertical .sidebar-menu {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 3px;
}

.layout-vertical .sidebar-menu .menu-item {
  height: 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  margin: 0 2px;
}

.layout-vertical .layout-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #fff;
}

.layout-vertical .layout-header {
  height: 10px;
  background: #fff;
  border-bottom: 1px solid #E4E7ED;
  display: flex;
  align-items: center;
  padding: 0 4px;
}

.layout-vertical .header-breadcrumb {
  width: 30%;
  height: 3px;
  background: #E4E7ED;
  border-radius: 2px;
}

.layout-vertical .layout-tabs {
  height: 6px;
  background: #fff;
  border-bottom: 1px solid #E4E7ED;
  display: flex;
  gap: 2px;
  padding: 1px 3px;
}

.layout-vertical .layout-tabs::before,
.layout-vertical .layout-tabs::after {
  content: '';
  width: 15%;
  height: 3px;
  background: #409EFF;
  border-radius: 1px;
  margin-top: 1px;
}

.layout-vertical .layout-tabs::after {
  background: #E4E7ED;
}

.layout-vertical .layout-content {
  flex: 1;
  background: #F0F2F5;
}

/* === 顶部菜单布局 === */
.layout-horizontal {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.layout-horizontal .layout-header {
  height: 12px;
  background: linear-gradient(180deg, #545c64 0%, #304156 100%);
  display: flex;
  align-items: center;
  padding: 0 4px;
  gap: 3px;
}

.layout-horizontal .header-logo {
  width: 8px;
  height: 6px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
}

.layout-horizontal .header-menu {
  flex: 1;
  display: flex;
  gap: 2px;
}

.layout-horizontal .header-menu .menu-item {
  width: 12%;
  height: 4px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 2px;
}

.layout-horizontal .layout-tabs {
  height: 6px;
  background: #fff;
  border-bottom: 1px solid #E4E7ED;
  display: flex;
  gap: 2px;
  padding: 1px 3px;
}

.layout-horizontal .layout-tabs::before,
.layout-horizontal .layout-tabs::after {
  content: '';
  width: 15%;
  height: 3px;
  background: #409EFF;
  border-radius: 1px;
  margin-top: 1px;
}

.layout-horizontal .layout-tabs::after {
  background: #E4E7ED;
}

.layout-horizontal .layout-content {
  flex: 1;
  background: #F0F2F5;
}

/* === 混合布局 === */
.layout-mix {
  display: flex;
  height: 100%;
}

.layout-mix .layout-left {
  width: 15%;
  background: linear-gradient(180deg, #545c64 0%, #304156 100%);
  display: flex;
  flex-direction: column;
  padding: 3px 2px;
}

.layout-mix .left-logo {
  height: 6px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 2px;
  margin-bottom: 3px;
}

.layout-mix .left-menu {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.layout-mix .left-menu .menu-item {
  height: 6px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
}

.layout-mix .left-menu .menu-item.active {
  background: rgba(64, 158, 255, 0.8);
}

.layout-mix .layout-right {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.layout-mix .layout-header {
  height: 10px;
  background: #fff;
  border-bottom: 1px solid #E4E7ED;
  display: flex;
  align-items: center;
  padding: 0 4px;
}

.layout-mix .header-breadcrumb {
  width: 30%;
  height: 3px;
  background: #E4E7ED;
  border-radius: 2px;
}

.layout-mix .layout-tabs {
  height: 6px;
  background: #fff;
  border-bottom: 1px solid #E4E7ED;
  display: flex;
  gap: 2px;
  padding: 1px 3px;
}

.layout-mix .layout-tabs::before,
.layout-mix .layout-tabs::after {
  content: '';
  width: 15%;
  height: 3px;
  background: #409EFF;
  border-radius: 1px;
  margin-top: 1px;
}

.layout-mix .layout-tabs::after {
  background: #E4E7ED;
}

.layout-mix .layout-body {
  flex: 1;
  display: flex;
}

.layout-mix .secondary-menu {
  width: 22%;
  background: #F8F9FA;
  border-right: 1px solid #E4E7ED;
  padding: 3px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.layout-mix .secondary-menu .menu-item {
  height: 5px;
  background: #E4E7ED;
  border-radius: 2px;
}

.layout-mix .layout-content {
  flex: 1;
  background: #F0F2F5;
}

/* 主题色 - 扁平化设计 */
.theme-colors {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.color-item {
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 5px;
  border-radius: 8px;
  border: 2px solid #e8eaed;
  background: #ffffff;
  display: flex;
  align-items: center;
  gap: 12px;
}

.color-item:hover {
  border-color: #dadce0;
  background: #f8f9fa;
}

.color-item.active {
  border-color: var(--el-color-primary);
  background: var(--el-color-primary);
}

.color-preview {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 2px solid rgba(0, 0, 0, 0.06);
}

.check-icon {
  color: #ffffff;
  font-size: 20px;
  font-weight: bold;
}

.color-name {
  flex: 1;
  font-size: 14px;
  color: #303133;
  font-weight: 500;
}

.color-item.active .color-name {
  color: #ffffff;
}

/* 设置项 */
.setting-items {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: #606266;
}

/* 操作按钮 */
.setting-actions {
  display: flex;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #DCDFE6;
}

.setting-actions .el-button {
  flex: 1;
}

</style>

